<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\9\26 0026
 * Time: 13:56
 */

namespace app\Lib\Model\Tag;


use app\Lib\Model\Img\Img;
use app\Lib\Model\Tag\HouseTypeTag\HouseTypeTag;
use app\Lib\Model\Tag\StyleTag\StyleTag;

abstract  class GetList implements \app\Lib\InterfaceIn\Tag\GetList
{
    private $keyword;
    private $page;
    private $limit;
    /** @var \app\Lib\Model\DbModel\StyleTag $table */
    public $table;
    public $isPage;
    public $type;
    /**
     * 搜索条件
     * @param $keyword
     * @return \app\Lib\InterfaceIn\Tag\GetList
     */
    public function setKeyword($keyword)
    {
        $this->keyword = $keyword;
        return $this;
    }

    /**
     * 页数
     * @param int $page
     * @return \app\Lib\InterfaceIn\Tag\GetList
     */
    public function setPage($page = 1)
    {
        $this->page = $page;
        return $this;
    }

    /**
     * 每页条数
     * @param int $limit
     * @return \app\Lib\InterfaceIn\Tag\GetList
     */
    public function setLimit($limit = 10)
    {
       $this->limit = $limit;
       return $this;
    }

    public function setIsPage($isPage){
        $this->isPage = $isPage;
        return $this;
    }

    public function setType($type)
    {
        $this->type = $type;
        return $this;
    }

    /**
     * 正式执行
     * @return array|string
     */
    public function run()
    {
        $map    =  $this->getMap();
        $model  =  $this->table;
        $count  =  $model->where($map)->count('*');

        if($this->isPage){
            return $count;
        }

        if(empty($count)){
            return ['list'=>[],'count'=>0];
        }

        $result = $model->where($map)->cache(60)->column($model->_id);
        $list   = $this->handleArray($result);
        return ['list'=>$list,'count'=>$count];
    }

    private function getMap(){
        $map = [];
        if($this->keyword){
            $model = $this->table;
            $map[$model->_tag_name] = $this->keyword;
        }
        return $map;
    }

    private function handleArray($result){
        $model = $this->table;
        $data  = [];
        foreach ($result as $v){
//            $updateIdClass  = $styleClass->getUpdateUidClass();
//            $updateUid      = $updateIdClass->getUid();
//            $updateAccount  = empty($updateUid)?'无':$updateIdClass->getAccount();
//            $updateTime     = ($updateAccount == '无')?'无':date('Y-m-d H:i:s',$styleClass->getUpdateTime());
            switch ($this->type){
                case 'HouseType':
                    $styleClass = new HouseTypeTag($v);
                    break;
                default:
                    $styleClass     = (new StyleTag($v));
                    break;
            }
            $imgClass = $styleClass->getImgClass();
            $imgPath ='无';
            if($imgClass instanceof  Img ){
                $imgPath = $imgClass->getFilePath();
            }
            $array = [
                $model->_id=>$styleClass->getTagId(),
                $model->_tag_name=>$styleClass->getTagName(),
                $model->_tag_status=>$styleClass->getTagStatusStr(),
                $model->_img_id=>$imgPath,
                $model->_create_id=>$styleClass->getCreateUidClass()->getAccount(),
                $model->_create_time=>date('Y-m-d H:i:s',$styleClass->getCreateTime()),
//                $model->_update_id=>$updateAccount,
//                $model->_update_time=>$updateTime,
            ];
            $data[]=$array;
        }
        return $data;
    }
}